﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace cadbom
{
    public partial class ProcessForm : Form
    {
        List<BasProcess> listView1 = new List<BasProcess>();
        List<BasProcess> listView2 = new List<BasProcess>();
        List<BasProcess> listView3 = new List<BasProcess>();
        List<BasProcess> list = new List<BasProcess>();
        Action<BasProcess> callback = null;
        private BindingList<BasProcess> basProcessList;
        public ProcessForm(BindingList<BasProcess> list, Action<BasProcess> callback)
        {
            this.callback = callback;
            this.list = list.ToList<BasProcess>();
            InitializeComponent();
        }
        public void changgeLocation()
        {
            //获取父窗口坐标，FormZone是父窗口的类名
            int right = Application.OpenForms["BomForm"].Right;
            int top = Application.OpenForms["BomForm"].Top;
            this.Location = new Point(right, top);
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            listView1 = (List<BasProcess>)list.FindAll(item => item.processType == "1");
            listView2 = (List<BasProcess>)list.FindAll(item => item.processType == "2");
            listView3 = (List<BasProcess>)list.FindAll(item => item.processType == "3");
            // 创建包含键值对的数据源
            var keyValuePairs = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>("1", "自制"),
                new KeyValuePair<string, string>("2", "外协"),
                new KeyValuePair<string, string>("3", "组合"),
            };
            DataGridViewTextBoxColumn processName = new DataGridViewTextBoxColumn();
            processName.HeaderText = "工艺";
            processName.DataPropertyName = "name";

            DataGridViewTextBoxColumn processName1 = new DataGridViewTextBoxColumn();
            processName1.HeaderText = "工艺";
            processName1.DataPropertyName = "name";
            DataGridViewTextBoxColumn processName2 = new DataGridViewTextBoxColumn();
            processName2.HeaderText = "工艺";
            processName2.DataPropertyName = "name";
            // 创建并添加列
            DataGridViewComboBoxColumn processAttributeColumn = new DataGridViewComboBoxColumn();
            processAttributeColumn.HeaderText = "属性";
            processAttributeColumn.DataPropertyName = "attribute";
            processAttributeColumn.DataSource = keyValuePairs;
            processAttributeColumn.DisplayMember = "Value"; // 显示文本
            processAttributeColumn.ValueMember = "Key";     // 值
            // 创建并添加列
            DataGridViewComboBoxColumn processAttributeColumn1 = new DataGridViewComboBoxColumn();
            processAttributeColumn1.HeaderText = "属性";
            processAttributeColumn1.DataPropertyName = "attribute";
            processAttributeColumn1.DataSource = keyValuePairs;
            processAttributeColumn1.DisplayMember = "Value"; // 显示文本
            processAttributeColumn1.ValueMember = "Key";     // 值
            // 创建并添加列
            DataGridViewComboBoxColumn processAttributeColumn2 = new DataGridViewComboBoxColumn();
            processAttributeColumn2.HeaderText = "属性";
            processAttributeColumn2.DataPropertyName = "attribute";
            processAttributeColumn2.DataSource = keyValuePairs;
            processAttributeColumn2.DisplayMember = "Value"; // 显示文本
            processAttributeColumn2.ValueMember = "Key";     // 值
            this.dataGridView1.Columns.Add(processName);
            this.dataGridView1.Columns.Add(processAttributeColumn);

            this.dataGridView1.DataSource = listView1;
            this.dataGridView2.Columns.Add(processName1);
            this.dataGridView2.Columns.Add(processAttributeColumn1);

            this.dataGridView2.DataSource = listView2;
            this.dataGridView3.Columns.Add(processName2);
            this.dataGridView3.Columns.Add(processAttributeColumn2);

            this.dataGridView3.DataSource = listView3;
            this.dataGridView1.Columns["id"].Visible = false;
            this.dataGridView1.Columns["processType"].Visible = false;
            this.dataGridView2.Columns["id"].Visible = false;
            this.dataGridView2.Columns["processType"].Visible = false;
            this.dataGridView3.Columns["id"].Visible = false;
            this.dataGridView3.Columns["processType"].Visible = false;
            this.dataGridView1.CellDoubleClick += dataGridView1_CellDoubleClick;
            this.dataGridView2.CellDoubleClick += dataGridView2_CellDoubleClick;
            this.dataGridView3.CellDoubleClick += dataGridView3_CellDoubleClick;
            this.Deactivate += ProcessForm_Deactivate;
            this.dataGridView1.RowPostPaint +=dataGridView1_RowPostPaint;
            this.dataGridView2.RowPostPaint +=dataGridView2_RowPostPaint;
            this.dataGridView3.RowPostPaint += dataGridView3_RowPostPaint;
            changgeLocation();
        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            SolidBrush b = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dgv.DefaultCellStyle.Font, b,
          e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            SolidBrush b = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dgv.DefaultCellStyle.Font, b,
          e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        private void dataGridView3_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            SolidBrush b = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dgv.DefaultCellStyle.Font, b,
          e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        private void ProcessForm_Deactivate(object sender, EventArgs e)
        {
            // 当窗口失去焦点时关闭窗口
            this.Close();
        }
        //鼠标双击时发送事件
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            BasProcess model = dataGridView1.CurrentRow.DataBoundItem as BasProcess;
            if (callback != null)
            {
                callback.Invoke(model);
            }
        }
        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            BasProcess model = dataGridView2.CurrentRow.DataBoundItem as BasProcess;
            if (callback != null)
            {
                callback.Invoke(model);
            }
        }
        private void dataGridView3_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            BasProcess model = dataGridView3.CurrentRow.DataBoundItem as BasProcess;
            if (callback != null)
            {
                callback.Invoke(model);
            }
        }

        private void ProcessForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            GValues.processFormClosed = true;
        }
    }
}
